package com.cqie.hospitalescort.config.filter;

import com.cqie.hospitalescort.DTO.AuthUser;
import com.cqie.hospitalescort.config.utils.JwtUtil;
import com.cqie.hospitalescort.enums.LoginTerminalEnum;
import com.cqie.hospitalescort.exception.BusinessException;
import com.cqie.hospitalescort.service.login.UserDetailService.CompositeUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtFilter extends OncePerRequestFilter {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private CompositeUserDetailsService compositeUserDetailsService;

    /**
     * token校验以及登录端校验
     *
     * @param request
     * @param response
     * @param filterChain
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String header = request.getHeader("Authorization");
        if (header != null && header.startsWith("Bearer ")) {
            String token = header.substring(7);
            String username = jwtUtil.extractSubject(token);
            if (!StringUtils.hasText(username)) {
                throw new BusinessException("用户名不能为空！");
            }

            //登录端验证
            String sourcePlatform = request.getHeader("Source-Platform");
            LoginTerminalEnum loginTerminalEnum = LoginTerminalEnum.getEnum(sourcePlatform);
            if (null == loginTerminalEnum) {
                throw new BusinessException("非法终端登录！");
            }

            switch (loginTerminalEnum) {
                case MINI_PROGRAM:
                    username = LoginTerminalEnum.MINI_PROGRAM.getCode() + "_" + username;
                    break;
                case BROWSER:
                    username = LoginTerminalEnum.BROWSER.getCode() + "_" + username;
                    break;
                default:
                    throw new BusinessException("非法终端登录！");
            }

            AuthUser userDetails = compositeUserDetailsService.loadUserByUsername(username);
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    userDetails,
                    null,
                    userDetails.getAuthorities()
            );
            authentication.setDetails(userDetails);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
        filterChain.doFilter(request, response);
    }
}
